﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace FootBallMatchProject.Piography.Admin
{
    public partial class AddEditCountry : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataClassLayer.Country country = new DataClassLayer.Country();
            DataClassLayer.CountryCollection countrys = new DataClassLayer.CountryCollection();
            if (!IsPostBack)
            {
                if (Request.QueryString["operation"] != null &&
                    Request.QueryString["operation"].ToString() == "Edit")
                {
                    
                    if (Request.QueryString["id"] != null &&
                    Request.QueryString["id"].ToString() != "")
                    {
                        country.ID = int.Parse(Request.QueryString["id"].ToString());
                        DataTable table = countrys.FillList(country);
                        if (table.Rows.Count == 1)
                        {
                            btnClear.Visible = false;
                            txtCountry.Text = table.Rows[0]["Name"].ToString();
                            txtGeneralInformation.Text = table.Rows[0]["Description"].ToString();
                        }
                    }
                }
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("CountryList.aspx");
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DataClassLayer.Country country = new DataClassLayer.Country();
            DataClassLayer.CountryCollection countrys = new DataClassLayer.CountryCollection();
            country.Name = txtCountry.Text;
         
            country.Description = txtGeneralInformation.Text;
            if (Request.QueryString["operation"] != null &&
                Request.QueryString["operation"].ToString() == "Edit")
            {
                if (Request.QueryString["id"] != null &&
                              Request.QueryString["id"].ToString() != "")
                {
                    country.ID = int.Parse(Request.QueryString["id"].ToString());
                    bool chkvalue = countrys.Edit(country);
                    if (chkvalue)
                    {
                        Response.Redirect("CountryList.aspx");
                    }
                    else
                    {
                        ///pop msg
                    }
                }
                else
                {
                    ///pop msg
                }
            }
            else
            {
                bool chkvalue = countrys.Add(country);
                if (chkvalue)
                {
                    Response.Redirect("CountryList.aspx");
                }
                else
                {
                    ///pop msg
                }
            }
        }
    }
}